﻿//加载列表
function initTable(queryData, pageNumber) {
    $("#maingrid").find("tbody").children().remove();
    $("#maingrid").ligerGrid({
        height: '100%',
        url: '/Scheduling/SchedulingType/Search?ram=' + Math.random(),
        //data: {
        //    "Total": 3, "Rows": [
        //        { "ID": 1, "Name": "班别名称1", "Enabled": 1, "Sort": "3","PostType":"1","Ratio":"1.02","WorkHours":"3.2"},
        //        { "ID": 2, "Name": "班别名称2", "Enabled": 0, "Sort": "2", "PostType": "2", "Ratio": "2.02", "WorkHours": "7.3" },
        //        { "ID": 3, "Name": "班别名称3", "Enabled": 0, "Sort": "1", "PostType": "1", "Ratio": "1.32", "WorkHours": "6.2" },
        //    ]
        //},
        parms: queryData,
        page: pageNumber,
        pageSize: 30,
        rownumbers: false,
        heightDiff: -5,
        columns: [
        { display: '班别名称', name: 'Name' },
        {
            display: '显示符号', name: 'Code', render: function (rowData) {
                if (rowData.Code == null) return "";
                return "<span style=\"font-weight:900;color:" + rowData.Color + "\">" + rowData.Code + "<span>";
            }
        },
        {
            display: '班别系数', name: 'Ratio',
            render: function (rowData) {
                if (rowData.Ratio == null) return (0.00);
                return rowData.Ratio.toFixed(2);
            }
        },
        { display: '上班时间', name: 'StatrWorkTime' },
        { display: '下班时间', name: 'EndWorkTime' },
        {
            display: '工时', name: 'WorkHours',
            render: function (rowData) {
                if (rowData.WorkHours == null) return (0.0);
                return rowData.WorkHours.toFixed(1);
            }
        },
        {
            display: '班别类型', name: 'CategoryType', minWidth: 80,
            render: function (rowData) {
                switch (rowData.CategoryType) {
                    case "1":
                        return "全院通用";
                    case "2":
                        return "自定义";
                    default:
                        return "未知";
                }
            }
        },
        {
            display: '所属病区', name: 'DepartmentCode', minWidth: 80,
            render: function (rowData) {
                if (rowData.CategoryType == 1) return "";
                if (rowData.DepartmentCode == 0) return "";
                return combobox.findTextByValue(rowData.DepartmentCode);
            }
        },
        { display: '排序', name: 'Sort' },
        {
            display: '状态', name: 'Enabled', render: function (rowData) {
                return rowData.Enabled == 1 ?
                    "<span onclick=\"enabled(" + rowData.Id + ")\" style=\"cursor:pointer;color:green;\">启用</span>" :
                    "<span onclick=\"enabled(" + rowData.Id + ")\" style=\"cursor:pointer;\" >禁用</span>";
            }
        },
        {
            display: '操作', name: '', minWidth:120,
            render: function (rowData) {
                return "<a style=\"cursor:pointer;\" class=\"a-edit\" onclick=\"edit(" + rowData.Id + ")\" >编辑</a> | <a style=\"cursor:pointer;\"  class=\"a-edit\" onclick=\"enabled(" + rowData.Id + ")\" >" + (rowData.Enabled == 1 ? "禁用" : "启用") + "</a> | <a style=\"cursor:pointer;\"  class=\"a-del\" onclick=\"del(" + rowData.Id + ")\" >删除</a>";
            }
        }]
    });
}
//启用状态
function enabled(id) {
    var url = "/Scheduling/SchedulingType/Enabled?ram=" + Math.random();
    $.post(url, {
        rnd: new Date().getMilliseconds(),
        Id: id
    }, function (data, textStatus) {
        if (textStatus == "success") {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                search(1);
                $.ligerDialog.success("操作成功");
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        } else { $.ligerDialog.error("程序错误"); }
    });
}
//搜索
function search(pageNumber) {
    var queryData = {
        type: $("#hdnScheduleType").val(),
        illareatype: $("#hdnillArea").val()
    }
    initTable(queryData, 1);
}
//添加/编辑
function edit(id) {
    var tit = id > 0 ? "编辑班别" : "新增班别"
    $.ligerDialog.open({
        height: 500,
        width: 400,
        title: tit,
        url: '/Scheduling/SchedulingType/Edit?id=' + id,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: {},
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnSearch").click();
            }
        }]
    });
    $(".l-dialog-buttons").hide();
}
//删除
function del(id) {
    $.ligerDialog.confirm('确定删除？', function (yes) {
        if (yes) {
            var url = "/Scheduling/SchedulingType/Delete";
            $.ajax({
                url: url,
                type: "POST",
                cache: false,
                data: { Id: id },
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        search(1);
                        $.ligerDialog.success("删除成功");
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}
//初始化
var combobox;
$(function () {
    $("#txtScheduleType").ligerComboBox({
        width: 150,
        data: [{ "ID": 0, "Name": "全部" }, { "ID": 1, "Name": "全院通用" }, { "ID": 2, "Name": "自定义" }],
        //url: "",
        isMultiSelect: false,
        valueField: "ID",
        textField: "Name",
        value: '0',
        valueFieldID: 'hdnScheduleType',
    });
    combobox = $("#txtillArea").ligerComboBox({
        width: 150,
        //data: [{ "value": 0, "text": "全部" }],
        url: '/Scheduling/SchedulingType/GetDepartmentList?ram=' + Math.random(),
        isMultiSelect: false,
        valueField: "value",
        textField: "text",
        value: '0',
        valueFieldID: 'hdnillArea',
        onSuccess: function (data) {
            combobox.insertItem({ "value": "0", "text": "全部" }, 0);
        }
    });
    $('#btnAdd').click(function () {
        edit(0);
    });
    $('#btnSearch').click(function () {
        search(1);
    });
    search(1);
});
